package com.wxm.remotemeter.service.impl;

import java.util.Map;

import com.talkweb.ncfw.data.Page;
import com.talkweb.ncfw.exceptions.BizBaseException;
import com.talkweb.ncfw.exceptions.BizMultiException;
import com.talkweb.ncfw.exceptions.DAOBaseException;
import com.talkweb.ncfw.orm.IGenericDAO;
import com.talkweb.ncfw.utils.AssertUtils;
import com.talkweb.ncfw.utils.PKUtils;
import com.wxm.remotemeter.persistence.entity.Meter;
import com.wxm.remotemeter.service.IMeterService;

/**
 * <p>文件名称: MeterServiceImpl.java</p>
 * <p>文件描述: 表具服务实现类</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2012-2-19</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class MeterServiceImpl implements IMeterService {
	private static final String DEFAULT_NAMESPACE = "com.wxm.remotemeter.persistence.dao.MeterMapper";
	private IGenericDAO genericDAO;

	public IGenericDAO getGenericDAO() {
		return genericDAO;
	}

	public void setGenericDAO(IGenericDAO genericDAO) {
		this.genericDAO = genericDAO;
	}

	public void add(Meter... meters) throws BizBaseException {
		String[] meteridArray = PKUtils.getPrimaryKeyArray(meters.length);
		for (int i = 0; i < meters.length; i++) {
			meters[i].setMeterid(meteridArray[i]);
		}
		try {
			this.genericDAO.insert(DEFAULT_NAMESPACE + ".insert", meters);
		} catch (DAOBaseException e) {
			if (meters.length == 0) {
				throw new BizBaseException("添加表具信息时发生错误", e);
			} else {
				throw new BizMultiException("添加表具信息时发生错误", e);
			}
		}
	}

	public void update(Meter meter) throws BizBaseException {
		try {
			this.genericDAO.update(DEFAULT_NAMESPACE + ".update", new Object[]{meter});
		} catch (DAOBaseException e) {
			throw new BizBaseException("修改表具信息时发生错误", e);
		}
	}

	public void deleteBatch(String[] meteridArray) throws BizBaseException {
		AssertUtils.notEmpty(meteridArray, " 表具ID不能为空");
		try {
			this.genericDAO.deleteByPkArray(DEFAULT_NAMESPACE + ".deleteByPkArray", meteridArray);
		} catch (DAOBaseException e) {
			throw new BizBaseException("修改表具信息时发生错误", e);
		}
	}

	public Meter get(String meterid) throws BizBaseException {
		Object meterObj = null;
		try {
			meterObj = this.genericDAO.load(DEFAULT_NAMESPACE + ".getByPrimaryKey", meterid);
		} catch (DAOBaseException e) {
			throw new BizBaseException("获取表具信息时发生错误，表具ID[" + meterid + "]", e);
		}
		return meterObj == null ? null : (Meter)meterObj;
	}

	public Page<Meter> find(Page<Meter> page, Map<String, Object> condition)
			throws BizBaseException {
		try {
			return this.genericDAO.find(DEFAULT_NAMESPACE + ".getCount", DEFAULT_NAMESPACE + ".find", 
					page, condition);
		} catch (DAOBaseException e) {
			throw new BizBaseException("查询表具时发生错误", e);
		}
	}

}

